﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_FieldProp
    /// </summary>
    public class DBLoader_FieldProp : IDBProc
    {
        GameDBManager db;
        private enum DBFields : int
        {
            AccountId = 0,
        }

        public DBLoader_FieldProp(GameDBManager dbManager)
        {
            db = dbManager;
        }

        BaseObject m_CBObject;

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {

            DbConnection conn = db.CreateConnection();
            DbCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select * from FieldPropResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int off = 0;
                    FieldPropBase fpb = new FieldPropBase();
                    fpb.drop_info[0] = new FieldPropBase.DropItemInfo();
                    fpb.drop_info[1] = new FieldPropBase.DropItemInfo();

                    fpb.nPropId = (uint)dr.GetInt32(off++);
                    int text_id = dr.GetInt32(off++);
                    int tooltip_id = dr.GetInt32(off++);
                    fpb.nType = dr.GetInt32(off++);
                    fpb.nLocalFlag = dr.GetInt32(off++);
                    fpb.nCastingTime = (uint)dr.GetInt32(off++)*100;
                    fpb.nUseCount = dr.GetInt32(off++);
                    fpb.nRegenTime = (uint)dr.GetInt32(off++)*100;
                    fpb.nLifeTime = (uint)dr.GetInt32(off++)*100;
                    float casting_range = (float)dr.GetDecimal(off++);
                    float target_fx_size = (float)dr.GetDecimal(off++);
                    fpb.nMinLevel = dr.GetInt32(off++);
                    fpb.nMaxLevel = dr.GetInt32(off++);
                    int limit_deva = dr.GetByte(off++);
                    int limit_asura = dr.GetByte(off++);
                    int limit_gaia = dr.GetByte(off++);
                    int limit_fighter = dr.GetByte(off++);
                    int limit_hunter = dr.GetByte(off++);
                    int limit_magician = dr.GetByte(off++);
                    int limit_summoner = dr.GetByte(off++);
                    fpb.nLimitJobId = dr.GetInt32(off++);
                    fpb.nActivateId[0] = dr.GetInt32(off++);
                    fpb.nActivateValue[0,0] = dr.GetInt32(off++);
                    fpb.nActivateValue[0,1] = dr.GetInt32(off++);
                    fpb.nActivateId[1] = dr.GetInt32(off++);
                    fpb.nActivateValue[1,0] = dr.GetInt32(off++);
                    fpb.nActivateValue[1,0] = dr.GetInt32(off++);
                    fpb.nActivateSkillId = dr.GetInt32(off++);
                    fpb.drop_info[0].code = dr.GetInt32(off++);
                    fpb.drop_info[0].ratio = (int)(double)dr.GetDecimal(off++) * 100000000;
                    fpb.drop_info[0].min_count = dr.GetInt32(off++);
                    fpb.drop_info[0].max_count = dr.GetInt32(off++);
                    fpb.drop_info[0].min_level = dr.GetInt32(off++);
                    fpb.drop_info[0].max_level = dr.GetInt32(off++);
                    fpb.drop_info[1].code = dr.GetInt32(off++);
                    fpb.drop_info[1].ratio = (int)(double)dr.GetDecimal(off++) * 100000000;
                    fpb.drop_info[1].min_count = dr.GetInt32(off++);
                    fpb.drop_info[1].max_count = dr.GetInt32(off++);
                    fpb.drop_info[1].min_level = dr.GetInt32(off++);
                    fpb.drop_info[1].max_level = dr.GetInt32(off++);

                    fpb.strScript = dr.GetString(off++);
                    string filename = dr.GetString(off++);

//                    fpb.nUnkDw1 = dr.GetInt32(off++);
//                    fpb.nUnkW1 = (short)dr.GetInt32(off++);
                    int eventID = 0;// dr.GetInt32(off++);



                    fpb.nLimit = 0;
                    if (limit_asura != 0)
                        fpb.nLimit |= 8;
                    if (limit_gaia != 0)
                        fpb.nLimit |= 0x10;
                    if (limit_deva != 0)
                        fpb.nLimit |= 4;
                    if (limit_hunter != 0)
                        fpb.nLimit |= 0x800;
                    if (limit_fighter != 0)
                        fpb.nLimit |= 0x400;
                    if (limit_magician != 0)
                        fpb.nLimit |= 0x1000;
                    if (limit_summoner != 0)
                        fpb.nLimit |= 0x2000;

                    if(eventID == 0 || eventID == GameRule.nCurrentEventID)
                        FieldPropManager.Instance.RegisterFieldPropBase(fpb);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load FieldPropResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_FieldProp.onProcess Exception: {0}", ex);
            }
            conn.Close();

            Globals.Log.Info("Total {0} FieldPropResource loaded...", count);
            return count != 0;
        }
    }

}

